import Vue from 'vue'
import VueRouter from 'vue-router'

import store from '@/store/index'

import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

Nprogress.configure({ showSpinner: false })

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: {
      name: 'Login'
    },
    component: () => import('@/views/Login/Login'),
    meta: {
      header: 0,
      Login: false
    }
  },

  // 登录
  {
    path: '/Login',
    name: 'Login',
    component: () => import('@/views/Login/Login'),
    meta: {
      header: 0,
      Login: false
    }
  },

  // 注册模块
  {
    path: '/Register',
    name: 'Register',
    component: () => import('@/views/Register/Register'),
    meta: {
      Login: false,
      header: 0
    }
  },

  //  修改密码
  {
    path: '/Reset',
    name: 'Reset',
    component: () => import('@/views/Reset/Reset'),
    meta: {
      Login: false,
      header: 0
    }
  },

  // 设置支付密码
  {
    path: '/Payment',
    name: 'Payment',
    component: () => import('@/views/Payment/Payment'),
    meta: {
      Login: true,
      header: 0
    }
  },

  // 分页模块
  {
    path: '/Home',
    name: 'Home',
    component: () => import('@/Home/Home'),
    children: [
      // 首页
      {
        path: '/Dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard/Dashboard'),
        meta: {
          header: 0,
          Login: false
        },
        hidden:  true
      },
      // 挖矿
      {
        path: '/Mining',
        name: 'Mining',
        component: () => import('@/views/Mining/Mining'),
        meta: {
          header: 0,
          Login: false
        }
      },
      // 挖矿详情
      {
        name: 'productDetails',
        path: '/productDetails',
        component: () => import('@/views/Mining/Details/Details')
      },
      // 资产托管
      {
        path: '/Custody',
        name: 'Custody',
        component: () => import('@/views/Custody/Custody'),
        meta: {
          header: 0,
          Login: false
        }
      },
      // 转换
      {
        path: '/Convert',
        name: 'Convert',
        component: () => import('@/views/Convert/Convert'),
        meta: {
          header: 1,
          Login: false
        }
      },
      // 用户中心
      {
        path: '/Login',
        name: 'User',
        component: () => import('@/views/User/User'),
        children: [
          // 资产
          {
            path: '/Assets',
            name: 'Assets',
            component: () => import('@/views/User/Assets/Assets'),
            meta: {
              header: 1,
              Login: true
            }
          },
          // 资产明细
          {
            path: '/userAssetsList',
            name: 'userAssetsList',
            component: () => import('@/views/User/Assets/userAssetsList/userAssetsList')
          },
          // 存款明细
          {
            path: '/Details',
            name: 'Details',
            component: () => import('@/views/User/Withdrawal/Details/Details'),
            meta: {
              header: 1,
              Login: true
            }
          },
          // 提取
          {
            path: '/Withdrawal',
            name: 'Withdrawal',
            component: () => import('@/views/User/Withdrawal/Withdrawal'),
            meta: {
              header: 1,
              Login: true
            }
          },
          // Products
          {
            path: '/Products',
            name: 'Products',
            component: () => import('@/views/User/Products/Products'),
            meta: {
              header: 1,
              Login: true
            }
          },
          //  Certification,
          {
            path: '/Certification',
            name: 'Certification',
            component: () => import('@/views/User/Certification/Certification'),
            meta: {
              header: 1,
              Login: true
            }
          },
          //  设置
          {
            path: '/Settings',
            name: 'Settings',
            component: () => import('@/views/User/Settings/Settings'),
            meta: {
              header: 1,
              Login: true
            }
          }
        ]
      },
      // 条款和条件
      {
        name: 'Condition',
        path: '/Condition',
        component: () => import('@/views/Agreement/Condition/Condition'),
        meta: {
          header: 1,
          Login: false
        }
      },
      // 隐私
      {
        name: 'Privacy',
        path: '/Privacy',
        component: () => import('@/views/Agreement/Privacy/Privacy'),
        meta: {
          header: 1,
          Login: false
        }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

// 路由进入拦截
router.beforeEach((to, form, next) => {
  Nprogress.start();
  if (to.meta.Login) {
    if (store.getters.getToken !== '') {
      next()
    } else {
      next('/Login')
    }
  } else {
    if (store.getters.getToken !== '') {
      if (to.path === '/' || to.path === '/Login' ||  to.path === '/Register') {
        next('/Dashboard')
      } else {
        next()
      }
    } else {
      next()
    }
  }
})

router.afterEach(() => {
  Nprogress.done()
})

// 重复点击路由
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
